package student

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/pkg/models"
	"project/pkg/validate"
)

func (h SHandler) SearchStudentByClass(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.SearchStudentByClassReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Student:SearchStudentByClass with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Student:SearchStudentByClass with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	res, total, err := h.M.StudentQueryClass(req.ClassID, req.Page, req.Size)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentQueryClass err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.Data = res
	resp.Count = total
	resp.SendSuccess(c)
}
func (h SHandler) SearchStudentByStudentId(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.SearchStudentByIdReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Student:SearchStudentByClass with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Student:SearchStudentByClass with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	res, err := h.M.StudentQueryStudentID(req.StudentID)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentQueryStudentID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.Data = res
	resp.SendSuccess(c)
}
func (h SHandler) SearchStudentByOpenId(c *gin.Context) {
	var info *models.UserInfoResp
	resp := new(models.RespData)
	req := new(models.SearchStudentByOpenIdReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Student:SearchStudentByClass with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Student:SearchStudentByClass with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//判断
	Sexist, student, err := h.M.StudentCheckExistByOpenID(req.OpenId)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentQueryStudentID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if !Sexist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	//赋值
	info = &models.UserInfoResp{
		WxOpenID:       student.WxOpenID,
		Name:           student.Name,
		StudentID:      student.StudentID,
		Phone:          student.Phone,
		ClassId:        student.ClassId,
		LeaveInfoCount: student.LeaveInfoCount,
		TeacherId:      student.TeacherId,
	}
	//补充其他信息
	Cexist, class, err := h.M.ClassCheckExistByID(student.ClassId)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentQueryStudentID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if Cexist {
		info.ClassName = class.Name
		info.Grade = models.GetClassGrade(class.Grade)
		info.College = class.College
	}
	Texist, teacher, err := h.M.TeacherCheckExistByID(student.TeacherId)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentQueryStudentID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if Texist {
		info.TeacherName = teacher.Name
	}
	resp.Data = info
	resp.SendSuccess(c)
}

func (h SHandler) SearchToClass(c *gin.Context) {
	resp := new(models.RespData)
	//page -size
	page, size, err := models.GetPageInfo(c)
	if err != nil {
		zap.L().Error("Student:SearchToClass models.GetPageInfo err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	classId := c.Query("class_id")
	if classId == "" {
		resp.SendCode(c, models.CodeInvalidParam)
		return
	}
	//查找学生
	classes, count, err := h.M.StudentQueryClass(classId, page, size)
	if err != nil {
		zap.L().Error("Student:SearchToClassh.M.StudentQueryClass err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.Data = classes
	resp.Count = count
	resp.SendSuccessToLayUi(c)
}
